package orderoid.formulaire;

import java.util.concurrent.ExecutionException;

import orderoid.singleton.SingletonClient;
import orderoid.soap.business.webServiceInscription;
import orderoid.soap.business.webServiceRecuperationAdresseClient;
import orderoid.soap.classeCommune.Adresse;
import orderoid.soap.classeCommune.Client;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class OrderoidInscriptionActivity extends Activity
{

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inscription);

		// textview
		EditText editTaxtMail = (EditText) findViewById(R.id.editTextMail);
		// editTaxtMail.setText(Variable);

		String email = SingletonClient.getInstance().getClient().getEmail();
		editTaxtMail.setText(email);

	}

	public void validerInscription(View view)
	{
		Intent choixRestaurant = new Intent(this, OrderoidChoixRestaurantActivity.class);
		// EditText editCivilite = (EditText) findViewById(R.id.SpinnerCivi);
		EditText editNom = (EditText) findViewById(R.id.EditTextNom);
		EditText editPrenom = (EditText) findViewById(R.id.EditTextPrenom);
		EditText editTelephone = (EditText) findViewById(R.id.editTextTel);
		EditText editTextMail = (EditText) findViewById(R.id.editTextMail);
		EditText editAdresse = (EditText) findViewById(R.id.editTextAdresse);
		EditText editComplement = (EditText) findViewById(R.id.editTextComplementAdr);
		EditText editCodepost = (EditText) findViewById(R.id.editTextCodepostal);
		EditText editVille = (EditText) findViewById(R.id.editTextVille);
		EditText editInscriptionPassword = (EditText) findViewById(R.id.editTextPassword);
		EditText editConfirm = (EditText) findViewById(R.id.editTextConfirmPassword);

		// String civilite = editCivilite.getText().toString();
		String nom = editNom.getText().toString();
		String prenom = editPrenom.getText().toString();
		String tel = editTelephone.getText().toString();
		String mail = editTextMail.getText().toString();
		String adresse = editAdresse.getText().toString();
		String complement = editComplement.getText().toString();
		String codepost = editCodepost.getText().toString();
		String ville = editVille.getText().toString();
		String inscriptionPassword = editInscriptionPassword.getText().toString();
		String confirm = editConfirm.getText().toString();

		if (inscriptionPassword.equals(confirm))
		{
			Client client = new Client();

			webServiceInscription inscription = new webServiceInscription();

			try
			{
				client = inscription.execute(nom, prenom, tel, mail, adresse, complement, codepost, ville, inscriptionPassword, "M.").get();
			} catch (InterruptedException e)
			{
				client = null;
				e.printStackTrace();
			} catch (ExecutionException e)
			{
				e.printStackTrace();
				client = null;
			}

			if (client == null)
			{
				Context context = getApplicationContext();
				CharSequence text = "Erreur durant la création d'un client";
				int duration = Toast.LENGTH_SHORT;

				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}
			else
			{
				SingletonClient.getInstance().setClient(client);

				webServiceRecuperationAdresseClient ws = new webServiceRecuperationAdresseClient();
				Adresse adres = new Adresse();

				try
				{
					adres = ws.execute(Integer.toString(SingletonClient.getInstance().getClient().getIdClient())).get();
				} catch (InterruptedException e)
				{
					adres = null;
					e.printStackTrace();
				} catch (ExecutionException e)
				{
					adres = null;
					e.printStackTrace();
				}
				if (adres == null)
				{
					Log.e("Adresse Error", "Une erreur est survenue lors de la recupération d'adresse");
				}
				else
				{
					SingletonClient.getInstance().setAdresse(adres);
					
					Context context = getApplicationContext();
					CharSequence text = "Client crée avec succes bienvenue : " + client.getPrenom();
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();

					startActivity(choixRestaurant);
				}
			}
		}
		// else le mot de passe et sa confirmation ne sont pas identique
		else
		{
			Context context = getApplicationContext();
			CharSequence text = " le mot de passe et confirmation ne sont pas identique, veuillez rééssayer";
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();

			startActivity(choixRestaurant);
		}
	}

	public void retourAccueil(View view)
	{
		Intent accueil = new Intent(this, OrderoidMainActivity.class);
		// lance la fenetre precedente
		startActivity(accueil);
	}

}